<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/19
 * Time: 18:03
 */
namespace app\api\controller;
use think\Db;
class My extends Base {
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        header('Access-Control-Allow-Origin:*');
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }

    public function userInfo(){
        extract(input());
        $field = 'user_username,user_image,user_gender,user_birthday,user_sign,user_phone';
        $field .= ',user_wx_token,user_qq_token,user_wb_token,user_ios_token';
        $where['user_id'] = $user_id;
        $res = Db::table('up_u_user')->where($where)
            ->field($field)
            ->find();
        return json(array('status'=>200,'msg'=>$res));

    }
    public function userInfoEdit(){
            extract(input());
        if(isset($username) && $username!=''){
            $data['user_username'] = $username;
        }
        if(isset($image) && $image!=''){
            $data['user_image'] = $image;
        }
        if(isset($user_gender) && $user_gender!=''){
            $data['user_gender'] = $user_gender;
        }
        if(isset($user_birthday) && $user_birthday!=''){
            $data['user_birthday'] = $user_birthday;
        }
        if(isset($user_sign) && $user_sign!=''){
            $data['user_sign'] = $user_sign;
        }
        $data['user_update_time'] = date('Y-m-d H:i:s');
        $where['user_id'] = $user_id;
        $res = Db::table('up_u_user')->where($where)->update($data);
        if($res){
            return json(array('status'=>200,'msg'=>"修改成功"));
        }else{
            return json(array('status'=>0,'msg'=>"修改失败"));
        }

    }
    public function question(){
        extract(input());
        $res = Db::table('up_question')
            ->where('q_status',1)
            ->order('q_time desc')
            ->page($page,20)
            ->field('q_id,q_title')
            ->select();
        return json(array('status'=>200,'msg'=>$res));
    }
    public function questionInfo(){
        extract(input());
        $res = Db::table('up_question')
            ->where('q_id',$q_id)
            ->find();
        return json(array('status'=>200,'msg'=>$res));
    }
    public function feedback(){
        extract(input());
        $data = [
            'feed_uid' => $user_id,
            'feed_content' => $content,
            'feed_time' => date('Y-m-d H:i:s'),
        ];
        $res = Db::table('up_feedback')->insert($data);
        if($res){
            return json(array('status'=>200,'msg'=>"成功"));
        }else{
            return json(array('status'=>0,'msg'=>"失败"));
        }
    }
    //    --------------------------消息通知----------------------------------------------------
    /**
     * 有没有未读消息
     */
    public function haveNoticeRed()
    {
        extract(input());
        $data = Db::table('up_notice')
            ->where(function ($query) use ($user_id) {
                $query->where('n_type', 1)->where('n_range', 1)->where('n_send', 2)->where('n_del_user_id','not like',"%,$user_id,%")->where('n_read_user_id','not like',"%,$user_id,%");
            })
            ->whereOr(function ($query) use ($user_id) {
                $query->where('n_type', 1)->where('n_range', 2)->where('n_send', 2)->where('n_user_id', 'like',"%,$user_id,%")->where('n_del_user_id','not like',"%,$user_id,%")->where('n_read_user_id','not like',"%,$user_id,%");
            })
            ->whereOr(function ($query) use ($user_id) {
                $query->where('n_type', '>',1)->where('n_user_id', $user_id)->where('n_read_user_id',0);
            })
            ->find();
        $have = 0;
        if($data){
            $have = 1;
        }
        return json(['status'=>200,'msg'=>$have]);
    }
    /**
     * 获取所有和我有关的消息
     * type：1是系统消息 2是喜欢 3是共同喜欢 4是动态点赞 5是评论点赞 6是评论
     * @return string
     */
    public function getNotice(){
        extract(input());
        if($type == 1) {
            $data = Db::table('up_notice')
                ->where(function ($query) use ($user_id) {
                    $query->where('n_type', 1)->where('n_range', 1)->where('n_send', 2)->where('n_del_user_id','not like',"%,$user_id,%");
                })
                ->whereOr(function ($query) use ($user_id) {
                    $query->where('n_type', 1)->where('n_range', 2)->where('n_send', 2)->where('n_user_id', 'like',"%,$user_id,%")->where('n_del_user_id','not like',"%,$user_id,%");
                })
                ->field('n_id,n_auto,n_auto_time,n_title,n_send_datetime,n_read_user_id')
                ->order('n_send_datetime DESC')
                ->page($page, 20)
                ->select();
            if($data){
                foreach ($data as $k=>$v){
                    if(!strstr($v['n_read_user_id'],",$user_id,")){
                        $data[$k]['n_read_user_id'] = 0;
                    }else{
                        $data[$k]['n_read_user_id'] = 1;
                    }
                    if($v['n_auto'] == 2){
                        $data[$k]['n_send_datetime'] = $v['n_auto_time'];
                    }
                }
            }
        }else{
            $field = 'n_id,n_auto,n_auto_time,n_title,n_send_datetime';
            $field .= ',n_type,n_app_id,n_status,n_user_id,n_read_user_id';
            $data = Db::table('up_notice')
                ->where('n_type', '<>',1)
                ->where('n_user_id', $user_id)
                ->field($field)
                ->order('n_send_datetime DESC')
                ->page($page, 20)
                ->select();
        }
        return json_encode(['status'=>200,'msg'=>$data]);
    }
    /**
     * 消息详情（当n_type为2时，n_status为1代表未评价，2 代表已评价）此时要去评价，n_appid就是plan_id
     */
    public function getNoticeInfo(){
        extract(input());
        $notice = Db::table('up_notice')
            ->where('n_id',$n_id)
            ->find();
        if($notice['n_type'] == 1){
            if(!strstr($notice['n_read_user_id'],",$user_id,")){
                $uids = $notice['n_read_user_id'] . $user_id . ',';
                Db::table('up_notice')
                    ->where('n_id', $n_id)
                    ->setField('n_read_user_id',$uids);
            }
        }else{
            if($notice['n_read_user_id'] == 0){
                Db::table('up_notice')
                    ->where('n_id',$n_id)
                    ->setField('n_read_user_id',1);
            }
        }
        if($notice['n_type'] == 2){
            $planinfo = Db::table('up_plan_info')->alias('p')
                ->join('up_u_user u','p.plan_teacher=u.user_id','LEFT')
                ->where('plan_id',$notice['n_app_id'])
                ->field('plan_id,plan_typename,plan_real_start,plan_real_end,user_username,plan_comment_star,plan_comment_content')
                ->find();
            $notice['planinfo'] = $planinfo;
        }
        return json_encode(['status'=>200,'msg'=>$notice]);
    }
    /**
     * 删除消息（清空）（待用）
     * type：1是系统消息 2是互动消息
     * @return string
     */
    public function delNoticeAll(){
        extract(input());
        if($type == 1) {
            $data = Db::table('up_notice')
                ->where(function ($query) use ($user_id) {
                    $query->where('n_type', 1)->where('n_range', 1)->where('n_del_user_id','not like',"%,$user_id,%");
                })
                ->whereOr(function ($query) use ($user_id) {
                    $query->where('n_type', 1)->where('n_range', 2)->where('n_user_id', 'like',"%,$user_id,%")->where('n_del_user_id','not like',"%,$user_id,%");
                })
                ->field('n_del_user_id,n_id')
                ->select();
            if($data){
                foreach ($data as $k=>$v){
                    $uids = $v['n_del_user_id'] . $user_id . ',';
                    Db::table('up_notice')
                        ->where('n_id', $v['n_id'])
                        ->setField('n_del_user_id',$uids);
                }
            }
        }else{
            Db::table('up_notice')
                ->where('n_user_id', $user_id)
                ->delete();
        }
        return json_encode(['status'=>200,'msg'=>'操作成功']);
    }
    /**
     * 删除消息：单个删除（待用）
     * @return string
     */
    public function deleteNoticeByIds()
    {
        extract(input());
        $n_id = trim($n_id,',');
        $notice = Db::table('up_notice')
            ->where('n_id','in',$n_id)
            ->field('n_type,n_range,n_del_user_id,n_id')
            ->select();
        foreach ($notice as $k=>$v){
            if($v['n_type'] == 1){
                $uids = $v['n_del_user_id'] . $user_id . ',';
                $res = Db::table('up_notice')
                    ->where('n_id', $v['n_id'])
                    ->setField('n_del_user_id',$uids);
            }else{
                $res = Db::table('up_notice')
                    ->where('n_id',$v['n_id'])
                    ->delete();
            }
        }
        if($res){
            return json_encode(['status'=>200,'msg'=>'操作成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'操作失败']);
        }
    }

}